<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            生产报工
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">产线</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 产线" id="workCenterCode" value=""
                            onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">日期</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择日期" class="aui-input" id="dateTime">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">工序</div>
                    <select id="opCode">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner" onclick="qtychange();">
                    <label><input class="aui-radio" type="radio" value="填写包装数" name="operateType2"></label>
                    <div class="aui-list-item-label">填写包装数</div>
                    <input type="text" class="aui-input" id="qty">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner" onclick="qtychange();">
                    <label><input class="aui-radio" type="radio" value="选择包装数" name="operateType2" checked></label>
                    <div class="aui-list-item-label">选择包装数</div>
                    <select id="selqty"  >
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">打印箱数</div>
                    <input type="text" class="aui-input" id="boxqty">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">是否合格 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="合格" name="operateType" checked>合格</label>
                        <label><input class="aui-radio" type="radio" value="不合格" name="operateType"> 不合格</label>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li>
        </ul>

        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>

        <div style="width:100%;margin-top:2%;"></div>

        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="report()">报工</div>

        <div style="width:100%;margin-top:2%;"></div>
        <div
            style="background-color:white; width: 100%;margin-top:2%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch;">

            <table class="table" style='width:1500px;' id="table1">
                <tbody style="display:block">
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center' style="width:40px;"></td>
                        <td algin='center' style="width:50px;">序号</td>
                        <td algin='center' style="width:130px;"> 工单号</td>
                        <td algin='center' style="width:130px;">产品物料编码</td>
                        <td algin='center' style="width:150px;;">产品物料名称</td>
                        <td algin='center' style="width:100px;">计划开始时间</td>
                        <td algin='center' style="width:100px;">计划结束时间</td>
                        <td algin='center' style="width:150px;">生产线编码</td>
                        <td algin='center' style="width:150px;">生产线名称</td>
                        <td algin='center' style="width:100px;">班次编码</td>
                        <td algin='center' style="width:130px;">班次名称</td>
                        <td algin='center' style="width:80px;">计划数量</td>
                        <td algin='center' style="width:80px;">完工数</td>
                        <td algin='center' style="width:80px;">不合格品数</td>
                    </tr>
                </tbody>
            </table>
            <table class="table" style='width:1500px;' id="table">
                <tbody id="tbodyHtml" style="display: block;height: 300px;overflow-y: scroll;">
                </tbody>
            </table>
        </div>
    </div>
    <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var scanRule = ""; //条码解析规则
    var flag ;
    var flag2 ;

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化
    apiready = function () {
        //默认显示当前日期
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        $("#dateTime").val(year + "-" + month + "-" + day);

        //产线
        $("#workCenterCode").focus();
        idSelector = "workCenterCode";
        //获取蓝牙列表
        getBluetoothCombox();

    }

    //查询工序下拉框
    function selectOpCode() {
        var lineCode = $("#workCenterCode").val()
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getOpCodeList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                lineCode: lineCode,
                code: 'NEXT_OPERATION_LIST'
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "<option value=''>--请选择--</option>";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].name + "'>" + list[i].name + "</option>";
                }
                $api.html($api.byId("opCode"), html);
            }
        });

    }


    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#dateTime").val(date);
                searchDetails();
            }
        });
    }

    function changeQty() {
        $("#qty").val("");
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });
        var produCode = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            produCode = row.find("[name='produCode']").html();

        });
        console.log(JSON.stringify(produCode));
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMrlQty";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                produCode: produCode,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                var list = eval(myobj.mbbProduPackRelList);
                //返回成功
                if (myobj.code == "0") {
                    if (list != undefined && list.length == 1) {
                        console.log()
                        var html = "<option value='"+list[0].packageQty+"'>" + list[0].packageQty + "</option>";
                        $api.html($api.byId("selqty"), html);
                    } else if (list != undefined && list.length > 1) {
                        var type = $("input[name='operateType2']:checked").val();
                        if (type == '选择包装数') {
                            var html = "<option value=''>--请选择--</option>";
                            for (var i = 0; i < list.length; i++) {
                                html += "<option value='" + list[i].packageQty + "'>" + list[i].packageQty + "</option>";
                            }
                            $api.html($api.byId("selqty"), html);
                        }
                    }
                    //                   $("#qty").val(myobj.packageQty);
                }

            }
        });


    }

    function qtychange() {
        var type = $("input[name='operateType2']:checked").val();
        if (type == '选择包装数') {
            $("#qty").val("");
            $("#qty").attr('disabled', true);
            changeQty();
        } else {
            $api.html($api.byId("selqty"), "");
            $("#qty").attr('disabled', false);
        }
    }



    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.searchBluetooth({}, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        // console.log(address);
        // api.showProgress({
        //     title: '连接中...',
        // });
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.connectBluetooth({
            address: address,
        }, function (ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：产线
            if (idSelector == 'workCenterCode') {

                //产线
                $("#workCenterCode").val(ret);

                //查询表格数据
                searchDetails();

                //查询工序编码
                selectOpCode();

                //2:箱号
            } else if (idSelector == 'opCode') {
                $("#opCode").val(ret);

            }
        });
    }

    //生产线监听回车事件
    $("#workCenterCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var workCenterCode = $(this).val();
        if (keycode == '13') {

            //生产线
            $("#workCenterCode").val(workCenterCode);

            //生产线值改变
            searchDetails();
            //查询工序编码
            selectOpCode();
        }
    });



    //查询表格数据
    function searchDetails() {
        var workCenterCode = $("#workCenterCode").val();
        var dateTime = $("#dateTime").val();
        if (workCenterCode == "") {
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkOrderTable";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                proddate: dateTime,
                linecode: workCenterCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);

                    var html = "";

                    //发货单明细数据存在
                    if (list.length > 0) {

                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                            html += "<td style='width:40px;'><input name='sub' type='checkbox'  onclick='changeQty()'/></td>";
                            html += "<td name='seqNo' style='width:50px;'>" + (i + 1) + "</td>";

                            html += "<td name='code' style='width:130px;' ondblclick = toNewPage('" + list[i].code + "')>" + list[i].code + "</td>";

                            html += "<td name='produCode' style='width:130px;'>" + list[i].produCode + "</td>";
                            html += "<td name='produName' style='width:150px;'>" + list[i].produName + "</td>";
                            html += "<td name='plannedBeginDate' style='width:100px;'>" + list[i].plannedBeginDate + "</td>";
                            html += "<td name='plannedEndDate' style='width:100px;'>" + list[i].plannedEndDate + "</td>";
                            html += "<td name='linecode' style='width:150px;'>" + (list[i].linecode == null ? "" : list[i].linecode) + "</td>";
                            html += "<td name='linename'  style='width:150px;'>" + (list[i].linename == null ? "" : list[i].linename) + "</td>";
                            html += "<td name='shiftCode'  style='width:100px;'>" + (list[i].shiftCode == null ? "" : list[i].shiftCode) + "</td>";
                            html += "<td name='shiftName'  style='width:130px;'>" + (list[i].shiftName == null ? "" : list[i].shiftName) + "</td>";
                            html += "<td name='qty'  style='width:80px;'>" + list[i].qty + "</td>";
                            html += "<td name='completeQty'  style='width:80px;'>" + (list[i].completeQty == null ? "" : list[i].completeQty) + "</td>";
                            html += "<td name='ext06'  style='width:80px;'>" + (list[i].ext06 == null ? "" : list[i].ext06) + "</td>";
                            html += "</tr>";
                        }

                    } else {

                    }

                    $api.html($api.byId("tbodyHtml"), html);

                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    $("#consignment").select();
                    idSelector = "consignment";
                    //清空表格数据
                    // $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }



    //让步放行，退回按钮事件
    function toPage(flag) {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });

        //选中行不为1
        if (count != 1) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }

        var id = "";
        var gyStatus = "";
        // var purchaseOrder = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            id = row.find("[name='id']").html(); //注意html()和val()
            gyStatus = row.find("[name='gyStatus']").html();
        });

        //1:放行:只能选中0待检和和2NG和4退或的
        if (flag == '3') {

            //判断状态
            if (gyStatus == "1") {
                api.toast({
                    msg: '状态必须是待检/NG/放行/退货!',
                    duration: 3000
                });
                return;
            }

            //2:退回:待检和NG
        } else if (flag == '4') {
            //判断状态
            if (gyStatus == "1" || gyStatus == '3' || gyStatus == '4') {
                api.toast({
                    msg: '状态必须是待检/NG!',
                    duration: 3000
                });
                return;
            }
        }

        //开发验证详细页面
        api.openWin({
            name: 'uwmSHVerification_input',
            url: 'uwmSHVerification_input.html',
            pageParam: {
                id: id,
                gyStatus: flag,
                name: api.winName,
            }
        });
    }


    //打印
    function printPDA(json, address) {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.printerPlatformBG({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {

                alert(JSON.stringify(err));
            }
        });
    }

    function toNewPage(code) {
        var opCode = $("#opCode").val();
        var address = $("#bluetooth").val();
        api.openWin({
            name: name,
            url: 'productionWorkOrder.html',
            pageParam: {
                name: "productionReport",
                title: title,
                workOrderCode: code,
                opCode: opCode,
                address: address,
            }
        });
    }

    //报工
    function report() {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });
        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }
        var list = [];

        check.each(function () { //遍历

            var row = $(this).parent("td").parent("tr");
            var code = row.find("[name='code']").html();
            var produCode = row.find("[name='produCode']").html();
            var produName = row.find("[name='produName']").html();
            var shiftCode = row.find("[name='shiftCode']").html();
            var completeQty = row.find("[name='completeQty']").html();

            list.push({
                "code": code,
                "produCode": produCode,
                "produName": produName,
                "shiftCode": shiftCode,
                "completeQty": completeQty,
            });
        });
        var opCode = $("#opCode").val();
        var qty = $("#qty").val();
        var selqty = $("#selqty").val();
        var boxqty = $("#boxqty").val();
        var proddate = $("#dateTime").val();
        var lineCode = $("#workCenterCode").val();
        var type = $("input[name='operateType']:checked").val();
        if (opCode == "" || opCode == null || opCode == undefined) {
            api.toast({
                msg: "请选择工序",
                duration: 3000
            });
            return;
        }
        if (qty == "" || qty == null || qty == undefined) {
            if (selqty == "" || selqty == null || selqty == undefined) {
                console.log(selqty)
                api.toast({
                    msg: "包装数不允许为空",
                    duration: 3000
                });
                return;
            } else {
                qty = selqty
            }

        }
        if (boxqty == "" || boxqty == null || boxqty == undefined) {
            api.toast({
                msg: "请填写打印箱数",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/report";
        var data = {
            values: {
                list: JSON.stringify(list),
                un: un,
                opCode: opCode,
                qty: qty,
                boxqty: boxqty,
                type: type,
                lineCode: lineCode,
                proddate: proddate,
            }
        };
        api.showProgress({
            title: '报工中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    searchDetails();
                    api.toast({
                        msg: '报工成功!',
                        duration: 3000
                    });
                    //返回json数据
                    var json = myobj.json;
                    print(json);

                } else if (myobj.code == "1") {

                    //清空表格数据
                    // $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
                api.hideProgress();
            }

        });
    }
    function print(json) {
        var address = $("#bluetooth").val();

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }

        //调用PDA打印
        printPDA(json, address);
    }


    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>